/**
 * Project: spring-security-simple-example
 * Created: 2016年8月30日
 * Copyright ©2011-2016 gopay Information Technology Co., LTD All Rights Reserved
**/
package com.gopay.common.util.cbrmb;

import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import org.springframework.util.CollectionUtils;

import com.gopay.common.util.cbrmb.enums.Orders;

/** 
 * @ClassName: CollectionUtils 
 * @Description: 
 * @author junjie.ge 
 * @date 2016年8月30日 上午11:38:27  
 */

public abstract class CbrmbCollectionUtils extends CollectionUtils{
    public static final String DEFAULT_DATE_PATTERN = "yyyy-MM-dd HH:mm:ss";
    public static final Orders DEFAULT_SORT_ORDER = Orders.ASC;
    
    public static boolean isNotEmpty(Collection<?> collection) {
        if (collection != null && !collection.isEmpty()) {
            return true;
        }
        return false;
    }
    
    public static void sortListByDate(List<?> list, final String datePropName) {
        sortListByDate(list, datePropName, DEFAULT_DATE_PATTERN, DEFAULT_SORT_ORDER);
    }

    public static void sortListByDate(List<?> list, final String datePropName, final Orders order) {
        sortListByDate(list, datePropName, DEFAULT_DATE_PATTERN, order);
    }

    public static void sortListByDate(List<?> list, final String datePropName, final String datePattern,
            final Orders order) {
        final SimpleDateFormat sdf = new SimpleDateFormat(datePattern);
        Collections.sort(list, new Comparator<Object>() {

            @Override
            public int compare(Object o1, Object o2) {
                Class<?> clazz1 = o1.getClass();
                Class<?> clazz2 = o2.getClass();
                Long o1Date = 0L;
                Long o2Date = 0L;
                try {
                    Field o1DateField = clazz1.getDeclaredField(datePropName);
                    o1DateField.setAccessible(true);
                    Object date1 = o1DateField.get(o1);
                    Field o2DateField = clazz2.getDeclaredField(datePropName);
                    o2DateField.setAccessible(true);
                    Object date2 = o2DateField.get(o2);
                    if (o1DateField.getType().isAssignableFrom(Date.class)) {
                        o1Date = ((Date) date1).getTime();
                    } else if (o1DateField.getType().isAssignableFrom(String.class)) {
                        o1Date = sdf.parse((String) date1).getTime();
                    }
                    if (o2DateField.getType().isAssignableFrom(Date.class)) {
                        o2Date = ((Date) date2).getTime();
                    } else if (o1DateField.getType().isAssignableFrom(String.class)) {
                        o2Date = sdf.parse((String) date2).getTime();
                    }
                } catch (Exception e) {
                    throw new IllegalStateException(e);
                }
                int ct = o1Date.compareTo(o2Date);
                switch (order) {
                case DESC:
                    ct = -ct;
                    break;

                default:
                    break;
                }
                return ct;
            }

        });
    }
}
